<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="父课程ID" prop="parentId">
        <el-input
          v-model="queryParams.parentId"
          placeholder="请输入父课程ID"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="课程编码" prop="courseCode">
        <el-input
          v-model="queryParams.courseCode"
          placeholder="请输入课程编码"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="课程名称" prop="coName">
        <el-input
          v-model="queryParams.coName"
          placeholder="请输入课程名称"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="课程售价" prop="money">
        <el-input
          v-model="queryParams.money"
          placeholder="请输入售价"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="观看人数" prop="people">
        <el-input
          v-model="queryParams.people"
          placeholder="请输入观看人数"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="教师名字" prop="teacherCode">
        <el-select v-model="queryParams.teacherCode" placeholder="请选择教师">
            <el-option
              v-for="item in teacherOptions"
              :key="item.teacherId"
              :label="item.teacherName"
              :value="item.teacherCode"
            ></el-option>
          </el-select>
      </el-form-item>
      <el-form-item label="类别名称" prop="caCode">
        <el-select v-model="queryParams.caCode" placeholder="请选择课程类别">
            <el-option
            v-for="item in categoryOptions"
            :key="item.caId"
            :label="item.caName"
            :value="item.caCode"
            ></el-option>
          </el-select>
      </el-form-item>
      <el-form-item label="评论编码" prop="coCode">
        <el-input
          v-model="queryParams.coCode"
          placeholder="请输入课程评论编码"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
	    <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['system:course:add']"
        >新增</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table
      v-loading="loading"
      :data="courseList"
      row-key="cId"
      default-expand-all
      :tree-props="{children: 'children', hasChildren: 'hasChildren'}"
    >
      <el-table-column label="父课程ID" prop="parentId" />
      <el-table-column label="课程编码" align="center" prop="courseCode" />
      <el-table-column label="课程名称" align="center" prop="coName" />
      <el-table-column label="课程视频" align="center" prop="courseVideoUrl" show-overflow-tooltip>
        <template slot-scope="scope">
              <div class="video">
                <el-dialog
                  title="播放课程视频"
                  width="72%"
                  append-to-body
                  top="20px"
                  :visible.sync="dialogVisible"
                  @closed="closeDialog"
                >
                  <h3>{{ courseVideoUrl }}</h3>

                  <video
                    width="100%"
                    autoplay="autoplay"
                    :src="playvideo"
                    :poster="playvideo"
                    controls="controls"

                    id="video"
                    preload
                  ></video>
                </el-dialog>
                <!-- 页面table显示的video标签 -->
                <video
                  :src="'/dev-api'+scope.row.courseVideoUrl"
                  width="100"
                  preload
                ></video>
                <i
                  class="el-icon-video-play playIcon"
                  @click="handleCheck(scope.row)"
                ></i>
              </div>
            </template>
      </el-table-column>
      <el-table-column label="课程售价" align="center" prop="money" />
      <el-table-column label="观看人数" align="center" prop="people" />
      <el-table-column label="教师编码" align="center" prop="teacherCode" />
      <el-table-column label="类别编码" align="center" prop="caCode" />
      <el-table-column label="评论编码" align="center" prop="coCode" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['system:course:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-plus"
            @click="handleAdd(scope.row)"
            v-hasPermi="['system:course:add']"
          >新增</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['system:course:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <!-- 添加或修改视频管理对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="父课程ID" prop="parentId">
          <treeselect v-model="form.parentId" :options="courseOptions" :normalizer="normalizer" placeholder="请选择父课程ID" />
        </el-form-item>
        <el-form-item label="课程编码" prop="courseCode">
          <el-input v-model="form.courseCode" placeholder="请输入课程编码" />
        </el-form-item>
        <el-form-item label="课程名称" prop="coName">
          <el-input v-model="form.coName" placeholder="请输入课程名称" />
        </el-form-item>
        <el-form-item label="课程视频">
          <fileUpload v-model="form.courseVideoUrl"/>
        </el-form-item>
        <el-form-item label="课程售价" prop="money">
          <el-input v-model="form.money" placeholder="请输入售价" />
        </el-form-item>
        <el-form-item label="观看人数" prop="people">
          <el-input v-model="form.people" placeholder="请输入观看人数" />
        </el-form-item>
        <el-form-item label="教师名字" prop="teacherCode">
          <el-select v-model="form.teacherCode" placeholder="请选择教师">
            <el-option
              v-for="item in teacherOptions"
              :key="item.teacherId"
              :label="item.teacherName"
              :value="item.teacherCode"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="类别名称" prop="caCode">
          <el-select v-model="form.caCode" placeholder="请选择课程类别">
            <el-option
            v-for="item in categoryOptions"
            :key="item.caId"
            :label="item.caName"
            :value="item.caCode"
            ></el-option>
          </el-select>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listCourse, getCourse, delCourse, addCourse, updateCourse, exportCourse } from "@/api/system/course";
import Treeselect from "@riophae/vue-treeselect";
import "@riophae/vue-treeselect/dist/vue-treeselect.css";

export default {
  name: "Course",
  components: {
    Treeselect
  },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 显示搜索条件
      showSearch: true,
      // 视频管理表格数据
      courseList: [],
      // 视频管理树选项
      courseOptions: [],
      //教师编码选项
      teacherOptions: [],
      //类别编码选项
      categoryOptions: [],
      // 视频播放弹窗
      dialogVisible: false, 
      // 存储用户点击的视频播放链接
      playvideo: null, 
      // 存储正在播放视频的名称
      playvideoName: null, 
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        parentId: null,
        courseCode: null,
        coName: null,
        courseVideoUrl: null,
        money: null,
        people: null,
        teacherCode: null,
        caCode: null,
        coCode: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        courseCode: [
          { required: true, message: "课程编码不能为空", trigger: "blur" }
        ],
        coName: [
          { required: true, message: "课程名称不能为空", trigger: "blur" }
        ],
        teacherCode: [
          { required: true, message: "教师编码不能为空", trigger: "change" }
        ],
        caCode: [
          { required: true, message: "课程分类编码不能为空", trigger: "change" }
        ],
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {

    // 查看
    handleCheck(row) {
      this.playvideo = '/dev-api'+row.courseVideoUrl // 存储用户点击的视频播放链接
      this.playvideoName = '/dev-api'+row.courseVideoUrl // 存储用户点击的视频播放链接
      this.dialogVisible = true
    },

    /** 查询视频管理列表 */
    getList() {
      this.loading = true;
      listCourse(this.queryParams).then(response => {
        this.courseList = this.handleTree(response.data, "cId", "parentId");
        this.loading = false;
      });
    },
    /** 转换视频管理数据结构 */
    normalizer(node) {
      if (node.children && !node.children.length) {
        delete node.children;
      }
      return {
        id: node.cId,
        label: node.coName,
        children: node.children
      };
    },
	/** 查询视频管理下拉树结构 */
    getTreeselect() {
      listCourse().then(response => {
        this.courseOptions = [];
        const data = { cId: 0, coName: '顶级节点', children: [] };
        data.children = this.handleTree(response.data, "cId", "parentId");
        this.courseOptions.push(data);
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        cId: null,
        parentId: null,
        courseCode: null,
        coName: null,
        courseVideoUrl: null,
        money: null,
        people: null,
        teacherCode: null,
        caCode: null,
        coCode: null,
        createTime: null,
        updateTime: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    /** 新增按钮操作 */
    handleAdd(row) {
      this.reset();
      this.getTreeselect();
      if (row != null && row.cId) {
        this.form.parentId = row.cId;
      } else {
        this.form.parentId = 0;
      };
      getCourse().then(response => {
        this.teacherOptions = response.teacher;
        this.categoryOptions = response.category;
        this.open = true;
        this.title = "添加视频管理";
      });
      
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      this.getTreeselect();
      if (row != null) {
        this.form.parentId = row.cId;
      }
      getCourse(row.cId).then(response => {
        this.form = response.data;
        this.teacherOptions = response.teacher;
        this.categoryOptions = response.category;
        this.open = true;
        this.title = "修改视频管理";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.cId != null) {
            updateCourse(this.form).then(response => {
              this.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addCourse(this.form).then(response => {
              this.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      this.$confirm('是否确认删除视频管理编号为"' + row.cId + '"的数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return delCourse(row.cId);
        }).then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        }).catch(() => {});
    }
  }
};
</script>
